package com.suke.czx.modules;

import javax.swing.*;
import java.awt.*;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.time.Duration;
import java.time.Instant;
import java.util.Timer;
import java.util.TimerTask;

/**
 * 应用模块名称: 按键测试程序
 * 代码描述: 按键测试程序,规定双击两次按键间隔需小于2秒,单次按键3秒以内为单击短按,单次长按3-10s为单击长按,超10s按下状态,点击开始按钮为释放
 * copyright: WEGO ALL RIGHTS RESERVED
 * company: WEGO
 *
 * @author: 颜米晨
 * @date: 2022/02/25 21:59
 */
public class KeyDemo extends JFrame {

    /**
     * 文本框
     */
    private TextField tf;

    /**
     * 按键开始时间
     */
    private Instant start;

    /**
     * 按键释放时间
     */
    private Instant end;

    /**
     * 用于判断双击时第二次按键间隔
     */
    private Instant firstPress;

    /**
     * 操作次数
     */
    private int ops;

    /**
     * 是否按下按钮布尔值
     */
    private boolean pressOnFlag;

    private KeyDemo() {
        // 设置窗口
        setSize(300, 200);
        // 设置窗口名称
        setTitle("按键检测程序");
        // 设置窗口位置
        setLocationRelativeTo(null);
        setLayout(null);
        // 设置关闭窗口按钮
        setDefaultCloseOperation(EXIT_ON_CLOSE);
        // 设置文本框
        tf = new TextField(30);
        tf.setBounds(0, 0, 100, 20);
        tf.setEditable(false);
        add(tf);
        // 设置按键按钮
        JButton btn = new JButton("按键");
        btn.setBounds(50, 40, 200, 40);
        add(btn);
        // 设置开始按钮
        JButton again = new JButton("每次检测先按开始");
        again.setBounds(50, 110, 200, 40);
        add(again);
        // 监听开始按钮操作,重置相应变量
        again.addActionListener(e -> {
            tf.setText(null);
            start = null;
            end = null;
            firstPress = null;
            ops = 0;
            if (pressOnFlag) {
                tf.setText("释放按键");
                pressOnFlag = false;
            }
        });
        // 监控按键操作
        btn.addMouseListener(new MouseAdapter() {

            @Override
            public void mousePressed(MouseEvent e) {
                ops ++;
                start = Instant.now();
                if (ops == 1) {
                    firstPress = start;
                }
            }

            @Override
            public void mouseReleased(MouseEvent e) {
                end = Instant.now();
                // 判断单击短按,单击长按,双击
                if (Duration.between(start, firstPress != null ? firstPress : Instant.EPOCH).toMillis() < 2000 && ops == 2) {
                    tf.setText("双击按键");
                } else if (Duration.between(start, end).toMillis() >= 3000 && Duration.between(start, end).toMillis() < 10000) {
                    tf.setText("长按按键");
                } else if (Duration.between(start, end).toMillis() < 3000) {
                    // 使用定时器延时展示单击按键输出提示,避免和双击按钮的显示产生交叉
                    new Timer().schedule(new TimerTask() {
                        @Override
                        public void run() {
                            if (ops == 1) {
                                tf.setText("单击按键");
                                cancel();
                            }
                        }
                    }, 2000, 1000);
                } else if (Duration.between(start, end).toMillis() >= 10000) {
                    // 模拟按下状态,当按键时长超过10s可认为按下状态
                    pressOnFlag = true;
                    tf.setText("按下按键");
                }
            }
        });
    }

    public static void main(String[] args) {
        EventQueue.invokeLater(() -> {
            try {
                KeyDemo keyDemo = new KeyDemo();
                keyDemo.setVisible(true);
            } catch (Exception e) {
                e.printStackTrace();
            }
        });
    }

}
